package com.zenithmind.news.websocket.handler;

import com.zenithmind.news.websocket.WebSocketMessageHandler;
import com.zenithmind.news.websocket.WebSocketSessionManager;
import com.zenithmind.news.websocket.WebSocketSubscriptionManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.Map;

/**
 * 订阅消息处理器 - 遵循单一职责原则
 * 专门处理订阅相关的WebSocket消息
 * 
 * @author ZenithMind Team
 * @since 2025-01-09
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class SubscriptionMessageHandler implements WebSocketMessageHandler {

    private final WebSocketSubscriptionManager subscriptionManager;

    @Override
    public String getSupportedMessageType() {
        return "SUBSCRIPTION";
    }

    @Override
    public WebSocketResponse handleMessage(WebSocketSession session, WebSocketRequest request, 
                                         WebSocketSessionManager.UserSessionInfo sessionInfo) {
        try {
            @SuppressWarnings("unchecked")
            Map<String, Object> data = (Map<String, Object>) request.getData();
            String action = (String) data.get("action");
            String type = (String) data.get("type");
            String target = (String) data.get("target");

            switch (action) {
                case "SUBSCRIBE":
                    return handleSubscribe(sessionInfo.getUserId(), type, target);
                case "UNSUBSCRIBE":
                    return handleUnsubscribe(sessionInfo.getUserId(), type, target);
                default:
                    return WebSocketResponse.error("UNKNOWN_ACTION", "未知的订阅操作");
            }
        } catch (Exception e) {
            log.error("处理订阅消息异常", e);
            return WebSocketResponse.error("SUBSCRIPTION_ERROR", "订阅操作失败");
        }
    }

    private WebSocketResponse handleSubscribe(String userId, String type, String target) {
        switch (type) {
            case "CATEGORY":
                subscriptionManager.subscribeCategory(userId, target);
                return WebSocketResponse.success("CATEGORY_SUBSCRIBED", 
                    Map.of("categoryId", target, "message", "分类订阅成功"));
            case "TAG":
                subscriptionManager.subscribeTag(userId, target);
                return WebSocketResponse.success("TAG_SUBSCRIBED", 
                    Map.of("tag", target, "message", "标签订阅成功"));
            default:
                return WebSocketResponse.error("UNKNOWN_TYPE", "未知的订阅类型");
        }
    }

    private WebSocketResponse handleUnsubscribe(String userId, String type, String target) {
        switch (type) {
            case "CATEGORY":
                subscriptionManager.unsubscribeCategory(userId, target);
                return WebSocketResponse.success("CATEGORY_UNSUBSCRIBED", 
                    Map.of("categoryId", target, "message", "取消分类订阅成功"));
            case "TAG":
                subscriptionManager.unsubscribeTag(userId, target);
                return WebSocketResponse.success("TAG_UNSUBSCRIBED", 
                    Map.of("tag", target, "message", "取消标签订阅成功"));
            default:
                return WebSocketResponse.error("UNKNOWN_TYPE", "未知的订阅类型");
        }
    }
}
